﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System.Collections.Generic;


namespace Steamrunner
{
    class MusicManager : GameComponent
    {
        List<string> musicList = new List<string>();

        static AudioEngine audioEngine;
        static WaveBank waveBank;
        static SoundBank soundBank;

        public AudioCategory musicAudioCategory;
        public AudioCategory soundEffectAudioCategory;

        Cue currentMusic;
        Cue rocketSound;

        IniFile config = null;

        public MusicManager(Game game)
            : base(game)
        {
        }

        public override void Initialize()
        {
            if (audioEngine == null)
            {
                audioEngine = new AudioEngine(@"Content\Audio\GameSound.xgs");
                
                musicAudioCategory = new AudioCategory();
                soundEffectAudioCategory = new AudioCategory();

                musicAudioCategory = audioEngine.GetCategory("Music");
                soundEffectAudioCategory = audioEngine.GetCategory("SoundEffect");
            }
            
            if (waveBank == null)
            {
                waveBank = new WaveBank(audioEngine, @"Content\Audio\Wave Bank.xwb");
            }
            
            if (soundBank == null)
            {
                soundBank = new SoundBank(audioEngine, @"Content\Audio\Sound Bank.xsb");
            }

            if (config == null)
            {
                config = new IniFile("config.ini");
                config.Parse();
            }

            float volumeM = config.GetValue("Volume", "Music");
            float volumeS = config.GetValue("Volume", "Sound");

            musicAudioCategory.SetVolume(volumeM);
            soundEffectAudioCategory.SetVolume(volumeS);

            base.Initialize();
        }

        public void ChangeMusicVolume(float volume)
        {
            musicAudioCategory.SetVolume(volume);
        }

        public void ChangeSoundVolume(float volume)
        {
            soundEffectAudioCategory.SetVolume(volume);
        }

        public override void Update(GameTime gameTime)
        {
            audioEngine.Update();

            base.Update(gameTime);
        }

        public void PlayMusic(string id)
        {
            if (currentMusic != null && currentMusic.IsPlaying)
            {
                currentMusic.Stop(AudioStopOptions.Immediate);
            }

            currentMusic = soundBank.GetCue(id);
            currentMusic.Play();
        }

        public void PauseMusic()
        {
            if (currentMusic.IsPlaying)
            {
                currentMusic.Pause();
            }
        }

        public void StopMusic()
        {
            if (currentMusic.IsPlaying)
            {
                currentMusic.Stop(AudioStopOptions.Immediate);
            }
        }

        public void AddMusic(string id)
        {
            musicList.Add(id);
        }

        public void RemoveMusic(string id)
        {
            if (musicList != null && musicList.Contains(id))
            {
                musicList.Remove(id);
            }
        }

        public void PlaySound(string id)
        {
            soundBank.PlayCue(id);
        }

        public void PlayRocket(string id)
        {
            if (rocketSound != null && rocketSound.IsPlaying)
            {
                rocketSound.Stop(AudioStopOptions.Immediate);
            }

            rocketSound = soundBank.GetCue(id);
            rocketSound.Play();
        }

        public void StopRocket()
        {
            if (rocketSound.IsPlaying)
            {
                rocketSound.Stop(AudioStopOptions.Immediate);
            }
        }
    }
}
